{extend name='public/base'/} {block name='yhgl'}layui-nav-itemed{/block} 
{block}{/block}
{block name='main'}
<div class="demoTable">
    搜索ID
    <div class="layui-inline">
        <input class="layui-input" name="id" id="demoReload" autocomplete="off">
    </div>
    <button class="layui-btn" data-type="reload" id="btnsearch">搜索</button>
</div>
<table id="user" lay-filter="user" class="layui-bg-green"></table>
{/block}
 {block name='js'}
<script type="text/javascript">
    layui.use('table', function() { 
        var table = layui.table;
        var $ = layui.$;
        table.render({
            elem: '#user',
            height: 400,
            url: '{:url("admin/user/getUserData")}',
            cols: [
                [{
                    field: 'id',
                    width: 80,
                    title: 'ID',
                    sort: true
                }, {
                    field: 'username',
                    title: '用户名'
                }, {
                    field: 'email',
                    title: '邮箱'
                }, {
                    field: 'role',
                    title: '角色',
                    edit: "text",
                    templet: function(d) {
                        if (d.role == 0) {
                            return '<span>成员</span>';
                        } else {
                            return '<span>管理员</span>';
                        }

                    }
                }, {
                    field: 'status',
                    title: '状态',
                    edit: "text",
                    templet: function(d) {
                        if (d.status == 1) {
                            return '<span style="color:green">正常</span>';
                        } else {
                            return '<span style="color:red">禁用</span>';
                        }
                    }
                }, {
                    field: 'score',
                    title: '学分',
                    sort: true
                }, {
                    field: 'ip',
                    title: 'ip'
                }, ]
            ],
            page: {
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'],
                limit: 10, //每个表格显示几条记录
                limits: [5, 10, 15], //每页显示几条
                groups: 3, //显示一个连续页码
                frist: true, //不显示首页
                last: false, //不显示尾页 
            },
            id: 'idTest'
        });
        $('#btnsearch').click(function() {
            // layer.alert($('#demoReload').val());
            // table.reload('idTest', {
            //     where: {//设定异步数据接口的额外参数
            //         id: $('#demoReload').val()
            //     },
            //     page: {
            //         curr: 1
            //     }

            // });

            table.reload('idTest', {
                where: { //设定异步数据接口的额外参数，任意设
                    id: $('#demoReload').val()
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            }); //只重载数据

        })
        table.on('edit(user)', function(obj) {
            var value = obj.value, //修改后的值
                data = obj.data, //得到所在行的键值 
                field = obj.field; //字段
            // layer.msg('[ID:' + data.id + '] ' + field + ' 字段更改为 ' + value);
            // layer.confirm("确认修改" + '[ID:' + data.id + '] ' + field + ' 字段更改为 ' + value, function() {
            layer.confirm("真的要修改么？", function() {
                $.ajax({
                    type: "get",
                    url: "{:url('admin/user/edit')}",
                    // data: "id=" + data.id + "&field=" + field + "&value" + value
                    data: data,
                    dataType: "json",
                    success: function(data) {
                        if (data.code == 200) {
                            layer.msg(data.msg, {
                                time: 500
                            }, function() {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(data.msg, {
                                time: 1000
                            }, function() {
                                window.location.reload();

                            })
                        }
                    }
                })
            })
        });


    });
</script>
{/block}